package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Category;
import com.example.domain.model.MyPage;
import com.example.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "分类列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<Category>> getList(@RequestBody MyPage myPage){
        Page<Category> page = new Page(myPage.getCurrent(),myPage.getSize());
        return R.success(categoryService.getList(page));
    }

    @ApiOperation(value = "查看分类")
    @GetMapping("/{id}")
    public R<Category> getInfo(@PathVariable String id){
        return R.success(categoryService.getById(id));
    }

    @ApiOperation(value = "新增分类")
    @PostMapping
    public R add(@RequestBody Category category){
        return categoryService.save(category) ? R.success() : R.error();
    }

    @ApiOperation(value = "更新分类")
    @PutMapping("/{id}")
    public R update(@PathVariable String id,@RequestBody Category category){
        return categoryService.update(id,category) ? R.success() : R.error();
    }

    @ApiOperation(value = "删除分类")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id){
        return categoryService.delete(id) ? R.success() : R.error();
    }

}
